<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
// use app\admin\model\Employ as EmployModel;//管理员模型
use app\admin\controller\Permissions;
class Employ extends Permissions
{
    /**
     * 招聘信息列表
     */
    public function index()
    {

        $name=trim($this->request->Post('name'));
        $state=$this->request->Post('state');
        if($name !='') $where['name']= ['like','%' . $name . '%'];
        if($state !='') $where['state']= ['=',$state];


        $employ = empty($where)?Db::name('employ')->order("id","desc")->paginate(10): Db::name('employ')->where($where)->order("id","desc")->paginate(10,true,['query'=>request()->Post()]);
        $this->assign('employ',$employ);
        // var_dump($news);
        return $this->fetch();
    }

    /**
     * 添加招聘信息
     */
    public function add(){

        //判断是不是操作数据库  
        if($this->request->isPost()){

            $post=$this->request->Post();
            $data=[
                    'name'=>$post['name'],
                    'location'=>$post['location'],
                    'yaoqiu'=>$post['yaoqiu'],
                    'zhize'=>$post['zhize'],
                    'state'=>$post['state'],
                    "create_time"=>date("Y-m-d H:i:s")
                ];
            $id=$this->request->Post("id");
            if($id==''){       
                $id=Db::name("employ")->insert($data);
                if($id==false) return $this->error('修改失败');   
            }else{
                $res = Db::name("employ")->where("id",$id)->update($data);
                if($res==false) return $this->error('修改失败');     
            }
            return $this->success("信息保存成功",'admin/employ/index');
        }else{
            return $this->fetch();
        }
    }

    /**
     * 修改页面
     */
    public function edit(){
        //获取对应数据id
        $id=$this->request->param("id");
        $employ=Db::name("employ")->where("id",$id)->find();
        // var_dump($employ);
        $this->assign("employ",$employ);
        return $this->fetch("employ/add");
    } 


     /**
     * 新闻删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('employ')->where('id',$id)->delete()) {
                return 1;
            } else {
                return 0;
            }
        }
    }


    /**
     * 人事部联系列表
     * 
     */
    public  function contact(){
         $post=trim($this->request->Post('post'));
        if($post !='') $where['post']= ['like','%' . $post . '%'];

        $contact=empty($where)?Db::name("contact")->order("id","desc")->paginate(10):Db::name("contact")->where($where)->order("id","desc")->paginate(10,true,['query'=>request()->Post()]);
        $this->assign("contact",$contact);
        return $this->fetch("employ/contact");
    }

        /**
     * 添加新闻类别
     */
    public function contact_add(){
        //实例化模型
        // $model=new NewsModel();

        //判断是不是操作数据库  
        if($this->request->isPost()){

            $post=$this->request->Post();
            $id=$this->request->Post("id");
            if($id==''){       
                $id=Db::name('contact')->insert($post);
                if($id==false) return $this->error('保存失败');   
            }else{
                $res =Db::name('contact')->where('id',$id)->update($post,$id);
                if($res==false) return $this->error('修改失败');     
            }
            return $this->success('保存成功','admin/employ/contact');
        }else{
            return $this->fetch();
        }
    }

    /**
     * 修改页面
     */
    public function contact_edit(){
        //实例化模型
        // $model=new NewsModel();
        //获取对应数据id
        $id=$this->request->param("id");
        $contact=Db::name("contact")->where("id",$id)->find();
        // var_dump($friendly);
        $this->assign("contact",$contact);
        return $this->fetch("employ/contact_add");
    } 


         /**
     * 新闻类别删除
     * @return [type] [description]
     */
    public function contact_delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('contact')->where('id',$id)->delete()) {
                return 1;
            } else {
                // addlog($id);//写入日志
                return 0;
            }
        }
    }

    
}
